#Kyle Bakken, Chelsea Evans, Adam Lamb - Group 6
#CS419-400
#Description: This program will filter a string parsed into xml tags and fill an Appointment object 
#with details on an advisor appointment, and update the email
#Input: This program takes a string
#Output: This program will output an Appointment object to
    # 1. The outlook py script


import os
import objects
import string
import re
import sys
import sqlhandler
from outlook_meeting import outlook_new, outlook_cancel

##Instantiate constants 'stringInput' and 'appointment' object
stringInput = ""
app = objects.Appointment

##Try to return the string between to xml tags
def parseXML(xmlString, first, last):
  try:
    start = xmlString.index(first) + len(first)
    end = xmlString.index(last, start)
    return xmlString[start:end]
  except ValueError:
    return ""

##Returns string in correct email format
def sendMessage(input):
  stringInput = str(input)
  counselorName = parseXML(stringInput, '<cname>', '</cname>')
  originEmail = parseXML(stringInput, '<orgmail>', '</orgmail>')
  sentDate = parseXML(stringInput, '<sentdate>', '</sentdate>')
  advisorEmail = parseXML(stringInput, '<advisoremail>', '</advisoremail>')
  subject = parseXML(stringInput, '<mailsubject>', '</mailsubject>')
  cancel = "Advising Signup %s CANCELLED\n" % counselorName
  studentName = parseXML(stringInput, '<name>', '</name>') + '\n'
  studentEmail = parseXML(stringInput, '<email>', '</email>') + '\n'
  day = parseXML(stringInput, '<day>', '</day>')
  date = parseXML(stringInput, '<date>', '</date>') + '\n'
  startTime = parseXML(stringInput, '<start>', '</start>')
  endTime = parseXML(stringInput, '<end>', '</end>') + '\n'
  contactMSG = "Please contact support@engr.oregonstate.edu if you experience problems"
  date = day + ", " + date
  
  ##Store parsed strings as appointment object parameters
  app.app_type = 'cancel'
  app.advisor = counselorName
  app.origin_email = originEmail
  app.made = sentDate
  app.advisor_email = advisorEmail
  app.subject = subject
  app.advisee = studentName
  app.email = studentEmail
  app.date = date
  app.start_time = startTime
  app.end_time = endTime
  
  ##String formatted as email message
  ##emailMessage = counselorName + 'From: ' + originEmail + 'Sent: ' + sentDate + 'To: ' + advisorEmail + 'Subject: ' + subject + cancel + 'Name: ' + studentName + 'Email: ' + studentEmail + 'Date: ' + date + 'Time: ' + startTime + ' - ' + endTime + contactMSG

  ##Send parameters to database
  outlook_cancel(app)

